<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<html >
<head>
    <meta charset="UTF-8">
    <title>Register - Online Judge</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" th:href="@{/css/styles.css}" >
    <script th:src="@{/js/jquery-1.7.2.js}"></script>
    <script th:src="@{/js/gdoj.js}"></script>
</head>
<body>
<div th:replace="common::web-navbar"></div>
<div class="container">
    <div class="content">
        <div class="roundbox" style="width: 420px;margin:68px auto;">
            <div class="roundbox-title">
                <div class="titled">[[#{register}]]</div>
            </div>
            <td class="register-box register-box-form">
                <table class="table-form">
                    <tr>
                        <td class="input"> <input  id="username" class="register-input"  name="username" maxlength="30" th:placeholder="#{register.username}" value="" /></td>
                    </tr>
                    <tr>
                        <td> <input  id="password" name="password" class="register-input" maxlength="30" type="password" th:placeholder="#{register.password}" value=""/></td>
                    </tr>
                    <tr>
                        <td> <input  id="password2"  name="repeat_password" class="register-input" type="password" maxlength="30" th:placeholder="#{register.confirm}"  value=""/></td>
                    </tr>
                    <tr>
                        <td > <input  id="email" name="email" class="register-input" th:placeholder="#{register.email}" value=""/></td>
                    </tr>
                    <tr>
                        <td >
                            <span class="fielderror"></span>
                        </td>
                    </tr>
                    <tr>
                        <td align="center" colspan="2"><input id="sub" type="submit" class="register-box-submit" th:value="#{register}"/> </td>
                    </tr>
                </table>
            </div>
    </div>
    <div th:replace="common::web-footer"></div>
</div>
<script type="text/javascript">
$(document).ready(function() {
    var registerFormSubmitListener = function () {
        var username = $("input[name=username]").val();
        var password = $("input[name=password]").val();
        var repeat_password = $("input[name=repeat_password]").val();
        var email = $("input[name=email]").val()
        var dataReq = {
            "username": username,
            "password": password,
            "repeat_password": repeat_password,
            "email": email
        }
        register = function () {
            var errMsg = ""
            $.ajax({
                url: "/api/register" ,
                type: "POST" ,
                contentType : 'application/json',
                data: JSON.stringify(dataReq),
                success:function (data) {
                    if (data.code != 200) {
                        var errList = data.data
                        for(var j in errList){
                            var eva = errList[j];
                            for(var key in eva){
                                errMsg += eva[key] + "<br>"
                            }
                        }
                        $(".fielderror").html(errMsg);
                    } else {
                        window.location = "/profile/" + data.data;
                    }
                },
                error:function () {
                },
                dataType:"json"
            })
        };
        register();
        return false;
    }

    $(".register-box-submit").click(function(){
        registerFormSubmitListener()
    });
});
</script>
</body>
</html>
